﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LCUV;
using System.Web.Configuration;
using Oracle.DataAccess.Client;
using System.Data;
using Telerik.Web.UI;

namespace sWarranty.WEB
{
    public partial class SOMSerialMgr : System.Web.UI.Page
    {
        public string _currentUserEmpNo = "";
        public string _currentUserAuthority = "";
        public string _currentUserRegion = "";

        //page nay chua xet theo curUserRegion

        protected void Page_Load(object sender, EventArgs e)
        {
            _currentUserEmpNo = GetUserLoginEmpNo();
            Session["UrltoFw"] = "SOMSerialMgr.aspx";

            if (_currentUserEmpNo == "")
                Response.Redirect("login.aspx");
            if (!IsPostBack)
            {

                LoadAU();
                txtToDate.Text = DateTime.Now.ToString("MM/dd/yyyy");
                int dateTOShow = 0 - Convert.ToInt32(DateTime.Now.DayOfWeek) + 1; //hom nay thu dateTOShow, thi chi hien den cot D dateTOShow
                txtFromDate.Text = DateTime.Now.AddDays(dateTOShow).ToString("MM/dd/yyyy");
            }

            btnImport.Attributes.Add("onclick", "openPopUp('SOMSerialUpload.aspx','open')");

           
        }
        protected string GetUserLoginEmpNo()
        {
            string t = "";
            try
            {
                if (Session["UserLoginInfo"] != null)
                {
                    Entities db = new Entities();
                    var u = (TB_USER[])Session["UserLoginInfo"];
                    int _UID = Convert.ToInt32(u.Single().ID);
                    var au = from ft in db.TB_USER_LEVEL
                             where ft.USER_ID == _UID
                             select ft;
                    int _le = Convert.ToInt32(au.Single().LEVEL_ID);
                    var le = from xx in db.TB_LEVEL
                             where xx.ID == _le
                             select xx;
                    _currentUserAuthority = le.Single().REMARK;
                    _currentUserRegion = u.Single().REGION_ID;
                    if (_currentUserRegion != "00")
                    {
                        DBAction.SetSelected(dropRegion, (u.Single().REGION_ID));
                        dropRegion.Enabled = false;
                    }
                    return u.Single().EMPNO;
                }
            }
            catch (Exception)
            {
                t = "";
            }
            return t;
        }
        protected void LoadAU()
        {
            Entities db = new Entities();
            var au = (from t in db.TB_CM_MODEL_CAT
                      select new { au = t.PROD_L1 }).Distinct();
            ListItem i = new ListItem();
            i.Value = "0";
            i.Text = "Select";
            dropAu.Items.Add(i);
            au = au.OrderBy(k => k.au);
            foreach (var item in au)
            {
                i = new ListItem();
                i.Value = item.au;
                i.Text = item.au.ToUpper();
                dropAu.Items.Add(i);
            }
        }
        protected void BuildTable(DateTime from, DateTime to, string p_shop_code, string p_shop_name,
            string p_prod_l1, string p_model, string p_suffix, string p_incentive_flag,
            string p_sms_yn, string curUserRegion, string region, string serialNo)
        {
            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SO_DLY_REG_INQ", "uiroi", "p_ds_date", from.ToString("yyyyMMdd"), "p_de_date", to.ToString("yyyyMMdd"),
                "p_shop_code", p_shop_code, "p_shop_name", p_shop_name, "p_prod_l1", p_prod_l1,
                "p_model", p_model, "p_incentive_flag", p_incentive_flag, "p_sms_yn", p_sms_yn
                , "p_user_region", curUserRegion, "p_region_id", region, "p_serial_no", serialNo, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
            RadGrid1.DataBind();
        }
        protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            //Page.ClientScript.RegisterStartupScript(this.GetType(), "click", "var btn=document.getElementById('btnFind');btn.click();", true);
            string region = "%";
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string p_shop_code = null;
            string p_shop_name = null;
            string p_prod_l1 = null;
            string p_model = null;
            string p_suffix = null;
            string p_incentive_flag = "%";
            string p_sms_yn = "%";
            string p_serial_no = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            if (!string.IsNullOrEmpty(txtShopCode.Text))
                p_shop_code = txtShopCode.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopName.Text))
                p_shop_name = txtShopName.Text.Trim();
            if (dropAu.SelectedValue != "0")
                p_prod_l1 = dropAu.SelectedValue;
            if (!string.IsNullOrEmpty(txtModel.Text))
                p_model = txtModel.Text.Trim();
            if (!string.IsNullOrEmpty(txtSuffix.Text))
                p_suffix = txtSuffix.Text.Trim();
            if (dropConfirmIncentive.SelectedValue != "0")
                p_incentive_flag = dropConfirmIncentive.SelectedValue;

            DataTable dt = new DataTable();
            dt = DBAction.runStore("PK_SOM_MST.PR_SO_DLY_REG_INQ", "uiroi", "p_ds_date", from.ToString("yyyyMMdd"), "p_de_date", to.ToString("yyyyMMdd"),
                "p_shop_code", p_shop_code, "p_shop_name", p_shop_name, "p_prod_l1", p_prod_l1,
                "p_model", p_model, "p_incentive_flag", p_incentive_flag, "p_sms_yn", p_sms_yn
                , "p_user_region", _currentUserRegion, "p_region_id", region, "p_serial_no", p_serial_no, null, null, null, null, null, null, null, null);
            try
            {
                if (dt.Rows.Count > 0 && !string.IsNullOrEmpty(dt.Rows[0]["Error"].ToString()))
                {
                    MessageBox.Show(dt.Rows[0]["Error"].ToString());
                    return;
                }
            }
            catch (Exception)
            {
                //Column 'Error' does not belong to table <-- ko co' loi j va co' data , thi ko lam j
            }
            RadGrid1.DataSource = dt;
        }
        protected void btnFind_Click(object sender, EventArgs e)
        {
            string region = "%";
            DateTime from = Convert.ToDateTime(txtFromDate.Text);
            DateTime to = Convert.ToDateTime(txtToDate.Text);
            string p_shop_code = null;
            string p_shop_name = null;
            string p_prod_l1 = null;
            string p_model = null;
            string p_suffix = null;
            string p_incentive_flag = "%";
            string p_sms_yn = "%";
            string p_serial_no = null;

            if (dropRegion.SelectedValue != "00")
                region = dropRegion.SelectedValue;
            if (!string.IsNullOrEmpty(txtShopCode.Text))
                p_shop_code = txtShopCode.Text.Trim();
            if (!string.IsNullOrEmpty(txtShopName.Text))
                p_shop_name = txtShopName.Text.Trim();
            if (dropAu.SelectedValue != "0")
                p_prod_l1 = dropAu.SelectedValue;
            if (!string.IsNullOrEmpty(txtModel.Text))
                p_model = txtModel.Text.Trim();
            if (!string.IsNullOrEmpty(txtSuffix.Text))
                p_suffix = txtSuffix.Text.Trim();
            if (dropConfirmIncentive.SelectedValue != "0")
                p_incentive_flag = dropConfirmIncentive.SelectedValue;
            if (!string.IsNullOrEmpty(txtSerialNo.Text.Trim()))
                p_serial_no = txtSerialNo.Text.Trim();

            BuildTable(from, to, p_shop_code, p_shop_name, p_prod_l1, p_model, p_suffix, p_incentive_flag, p_sms_yn, _currentUserRegion, region, p_serial_no);
        }
        protected void RadGrid1_GroupsChanging(object sender, GridGroupsChangingEventArgs e)
        {
            if (e.Action == GridGroupsChangingAction.Group)
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = false;
            else
                RadGrid1.MasterTableView.GetColumnSafe(e.Expression.GroupByFields[0].FieldName).Visible = true;
        }

        protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
        {
            if (e.Column is GridBoundColumn)
            {
                (e.Column as GridBoundColumn).DataFormatString = "<nobr>{0}</nobr>"; //auto width
                (e.Column as GridBoundColumn).HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
                (e.Column as GridBoundColumn).HeaderStyle.Font.Bold = true;      
   
                if (e.Column.HeaderText == "AMOUNT" || e.Column.HeaderText == "MRP")
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                if (e.Column.HeaderText == "PRODUCT")
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Center;
                if (e.Column.HeaderText == "CFM_FLAG")
                    (e.Column as GridBoundColumn).ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            }

        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            RadGrid1.ExportSettings.IgnorePaging = true;
            RadGrid1.ExportSettings.OpenInNewWindow = true;
            RadGrid1.ExportSettings.FileName = "SOM_daily_" + DateTime.Now.ToString("yyyyMMdd") ;
            RadGrid1.MasterTableView.ExportToExcel();
        }
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (_currentUserRegion != "00")
            {
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("REGION");
                if (column != null)
                {
                    column.Visible = false;
                }
            }
        }
    }
}
